﻿using System;
using System.Collections.Concurrent;
using System.Linq;
namespace EventBus.Common
{
    public static class TypeNameProvider
    {
        private static readonly ConcurrentDictionary<string, Type> _dict = new ConcurrentDictionary<string, Type>();

        public static Type GetType(string typeName)
        {
            var assembly = RuntimeHelper.GetAssembly("Conference.Domain");
            if (assembly == null)
            {
                throw new DllNotFoundException($"the dll Conference.Domain not be found");
            }
            Type t;
            if (_dict.TryGetValue(typeName, out t))
            {
                return t;
            }

            var type = assembly.GetTypes().Where(e => e.Name == typeName).FirstOrDefault();
            if (type != null)
            {
                _dict.TryAdd(typeName, type);
                return type;
            }
            return null;
        }
    }
}
